// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
var Game = require("Game");
cc.Class({
    extends: cc.Component,

    properties: {
        endX:0,//最终飞向的X坐标
        endY:0,//最终飞向的Y坐标
        flyTime:0,//飞行时间
        flySpeed:0,//飞行速度
        flyLength:0,//爆炸飞行距离
    },
    
    //金币被炸飞行
	goldenFlyAction: function(){
		//console.log(this.node.x+"aaaaaaaaaaa"+this.node.y);
		var fly = cc.moveBy(this.flyTime, cc.v2(this.endX, this.endY)).easing(cc.easeCubicActionOut());//快到慢
		//var flyUp = cc.moveBy(this.flyTime, cc.v2(0, this.flyLength)).easing(cc.easeCubicActionIn());
	    return fly;
	},
	
	/**
	 * 金币炸飞后  飞到顶部
	 */
	goldenFlyToTopAction: function(){
		//console.log(this.node.x+"bbbbbbbbbbbbb"+this.node.y);
//		var endPos = this.node.convertToNodeSpaceAR(cc.v2(430,750));
//		var startPos = this.node.convertToNodeSpaceAR(cc.v2(this.node.x,this.node.y));
//		var subPos = startPos.sub(endPos); // 2.X版本是 p1.sub(p2);
//		var aimPos = this.worldConvertLocalPointAR(this.node,cc.v2(210,360));
		var fly = cc.moveTo(this.flyTime, cc.v2(-210,375)).easing(cc.easeCubicActionIn());//慢到快
		//var fly = cc.moveBy(this.flyTime, cc.v2(180-this.endX, 300-this.endY)).easing(cc.easeCubicActionIn());//慢到快
		//var flyUp = cc.moveBy(this.flyTime, cc.v2(0, this.flyLength)).easing(cc.easeCubicActionIn());
//	    return fly;
		this.node.runAction(fly);
	},

	/**
	 * 把一个世界坐标的点，转换到某个节点下的坐标
	 * 原点在node中心
	 * @param {*} node 
	 * @param {*} worldPoint 
	 */
	worldConvertLocalPointAR: function (node, worldPoint) {
	    if (node) {
	        return node.convertToNodeSpaceAR(worldPoint);
	    }
	    return null;
	},
	

    onLoad () {
    	this.node.runAction(this.goldenFlyAction());//金币飞行移动
    	this.scheduleOnce(function(){//两段移动 需要等地一段完成在执行第二段,同时执行是异步操作
		    this.goldenFlyToTopAction();   //需要延时操作的内容
		},0.6);
    },

    start () {

    },

    update (dt) {
    	if(this.node.x<-200 && this.node.y>360){
    		this.node.destroy();
    		//var oldNum = Game.inst.goldnum.string;
    		//var a = Game.inst.goldnum;
    		//a.string = 'nihao';
    		//this.scoreDisplay.string = 'Score: ' + this.score;
    		var gnNode=cc.find("Canvas/goldNum");
			var gn = gnNode.getComponent(cc.Label);
			var oldNum = gn.string;
			gn.string = Number(oldNum) + 1 + '';
    	}
    },
});
